<template>
  <div class="btn_wrap">
    <button class="sub_btn" @click="subHandle">-</button>
    <input :value="value? value:1" type="text" @change="inpChange" class="inp">
    <button class="add_btn" @click="addHandle">+</button>
  </div>
</template>

<script>
export default {
  props: {
    value: {
      type: Number,
      require: true
    }
  },
  methods: {
    addHandle () {
      this.$emit('input', this.value + 1)
    },
    subHandle () {
      if (this.value <= 1) { return }
      this.$emit('input', this.value - 1)
    },
    inpChange (e) {
      const num = +e.target.value
      // 判断用户输入是否合法
      if (isNaN(num)) {
        e.target.value = this.value
        return
      }
      this.$emit('input', num)
    }
  }
}
</script>

<style lang="less" scoped>
.btn_wrap{
  width: 110px;
  .sub_btn,.add_btn{
    width: 30px;
    height: 30px;
    outline: none;
    border: none;
    background-color: #efefef;
  }
  .inp {
    margin: 0 5px;
    width: 40px;
    height: 30px;
    text-align: center;
    outline: none;
    border: none;
    background-color: #efefef;

  }
}
</style>
